import Vue from 'vue';
import Vuex from 'vuex';
import HttpClient from '@/phr/common/HttpClient';
import conf from '@/phrConf'
import cacheService from '@/phr/service/cache/CacheService'

Vue.use(Vuex);


export default new Vuex.Store({
  state: {
    //全局TOKEN
    token:getInitToken(),
    //用于因为TOKEN失效而跳到登录界面，然后登录成功后将要跳转的界面
    targetPath:null,
  },
  mutations: {
    setToken(state,token){
      state.token = token;
      cacheService.setItem("token",token)
    },
    setTargetPath(state,path){
        state.targetPath = path;
    },
    resetTargetPath(state){
        state.targetPath = null;
    }
  },
  actions: {

  },
});

/**
 * 获取初始的TOKEN
 * 如果在DEV状态，我们会使用conf的token进行初始化，如果我们不在DEV状态，将会尝试从本地缓存中获取
 * @returns {string}
 */
function getInitToken():string{
    if (conf.dev){
      return conf.token;
    }
    return cacheService.getItem("token")
}
